package kr.saeildamil.androidstudy_second28;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.util.ArrayList;

import kr.saeildamil.androidstudy_second.R;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;

public class Pull_parserActivity extends Activity {
	ListView list;
	ProgressDialog progressDlg;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ch28_xmlparser);
		list = (ListView) findViewById(R.id.listView);
		new ParsingXml().execute("order.xml");
	}
	
	private class ParsingXml extends AsyncTask<String, Void, ArrayList<Item>>{
		protected void onPreExecute() {
			//super.onPreExecute();
			progressDlg = ProgressDialog.show(Pull_parserActivity.this, "Wait", "order.xml Parsing...");
		}
		protected void onPostExecute(ArrayList<Item> result) {
			//super.onPostExecute(result);
			MyAdapter adapter = new MyAdapter(result, Pull_parserActivity.this);
			progressDlg.dismiss();
			list.setAdapter(adapter);
		}

		protected ArrayList<Item> doInBackground(String... params) {
			StringBuilder sb =  new StringBuilder();
			String str="";
			try {
				BufferedReader br = 
					new BufferedReader(new InputStreamReader(openFileInput(params[0])));
				while( (str = br.readLine()) != null){
					sb.append(str);
				}
				br.close();
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return parsingXml(sb.toString());
		}
		
	}//end of class AsyncTask
	
	public ArrayList<Item> parsingXml(String xml){
		ArrayList<Item> arItem = new ArrayList<Item>();
		Item itemClass = null;
		boolean initem = false;

		try {
			XmlPullParserFactory factory =  XmlPullParserFactory.newInstance();
			XmlPullParser parser = factory.newPullParser();
			
			parser.setInput(new StringReader(xml));
			int eventType = parser.getEventType();
			
			while(eventType != XmlPullParser.END_DOCUMENT){
				switch(eventType){
				case XmlPullParser.START_DOCUMENT:
					Log.d("aaaaaaaaaaaaa", "START_DOCUMENT");
					break;
				case XmlPullParser.END_DOCUMENT:
					Log.d("aaaaaaaaaaaaa", "END_DOCUMENT");
					break;
				case XmlPullParser.START_TAG:
					Log.d("aaaaaaaaaaaaa", "START_TAG");
					if (parser.getName().equalsIgnoreCase("item")){
						initem = true;
						itemClass = new Item();
					}
					//속성 추출
					for(int i=0; i<parser.getAttributeCount();i++){
						Log.d("bbbbb", parser.getAttributeValue(i));
						if (parser.getAttributeName(i).equalsIgnoreCase("price")){
							itemClass.setItemPrice(Integer.parseInt(parser.getAttributeValue(i)));
						}else{
							itemClass.setMakeName(parser.getAttributeValue(i));
						}
					}
					
					break;
				case XmlPullParser.END_TAG:
					Log.d("aaaaaaaaaaaaa", "END_TAG");
					if (parser.getName().equals("item")){
						arItem.add(itemClass);
					}
					break;
				case XmlPullParser.TEXT:
					if (initem){
						Log.d("bbbbb", parser.getText());
						itemClass.setItemName(parser.getText());
						initem=false;
					}
					Log.d("aaaaaaaaaaaaa", "TEXT");
					break;
				}
				eventType = parser.next();
			}
		} catch (XmlPullParserException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return arItem;
	}
}
























